home *** CD-ROM | disk | FTP | other *** search
/ PC Player 2004 May / pc player 2004-05.iso / Demos / FarCry / Data1.cab / _3AA4161F96BC4881958274742FEC0588 < prev    next >
Encoding:
Text File  |  2004-01-06  |  9.4 KB  |  420 lines

  1. ----------------------------------------------------------------------------
  2. --
  3. -- Description :        General purpose AI trigger
  4. --
  5. -- Created by Petar 
  6. --
  7. ----------------------------------------------------------------------------
  8. AITrigger = {
  9.     type = "Trigger",
  10.  
  11.     Properties = {
  12.         DimX = 5,
  13.         DimY = 5,
  14.         DimZ = 5,
  15.         bEnabled=1,
  16.         aianchorAIAction = "",
  17.         fAnchorRadius = 0,
  18.         ExitDelay=0,
  19.         bStopAI = 0,
  20.         bTriggerOnce = 0,
  21.         fileModel = "",
  22.         bAILadder = 0,
  23.  
  24.         Signal = {
  25.             bSendSignal = 0,
  26.             bReadibility = 0,
  27.             signalText = "ALARM_ON",
  28.             signalRadius = 20,
  29.         },
  30.                 
  31.     },
  32.  
  33.     Waiting = {},
  34.     NumWaiting = 0,
  35.     NumInside = 0,
  36.     bExplicitlyDisabled = 0,
  37.     
  38.     Editor={
  39.         Model="Objects/Editor/T.cgf",
  40.     },
  41. }
  42.  
  43. function AITrigger:OnPropertyChange()
  44.     self:OnReset();
  45.     if (self.Properties.fileModel~=self.LastModelLoaded) then
  46.         if (self.Properties.fileModel~="") then
  47.             self:LoadObject(self.Properties.fileModel,0,0);
  48.             self:DrawObject(0,1);
  49.             self.LastModelLoaded = self.Properties.fileModel;
  50.         end
  51.     end
  52.  
  53. end
  54.  
  55. function AITrigger:OnInit()
  56.     self.Who = nil;
  57.     self.Entered = 0;
  58.     self.bLocked = 0;
  59.     self.bTriggered = 0;
  60.  
  61.     self:RegisterState("Inactive");
  62.     self:RegisterState("Empty");
  63.     self:RegisterState("Occupied");
  64.     self:OnReset();
  65.  
  66.     if (self.Properties.fileModel~="") then
  67.         self:LoadObject(self.Properties.fileModel,0,0);
  68.         self:DrawObject(0,1);
  69.         self.LastModelLoaded = self.Properties.fileModel;
  70.     end
  71.  
  72.     self:EnableUpdate(0);
  73.     self:SetUpdateType( eUT_Physics );
  74.     self:TrackColliders(1);
  75. end
  76.  
  77. function AITrigger:OnShutDown()
  78. end
  79.  
  80. function AITrigger:OnSave(stm)
  81.     --WriteToStream(stm,self.Properties);
  82.     stm:WriteInt(self.bExplicitlyDisabled);
  83. end
  84.  
  85.  
  86. function AITrigger:OnLoad(stm)
  87.     self.bExplicitlyDisabled = stm:ReadInt();
  88.     -- voodoo trick to make sure we are in the correct state
  89.     self:GotoState("Empty");
  90.     self:GotoState("Inactive");
  91.  
  92. end
  93.  
  94. function AITrigger:OnReset()
  95.     self:KillTimer();
  96.     self.bTriggered = 0;
  97.     self.bExplicitlyDisabled = 0;
  98.  
  99.     local Min = { x=-self.Properties.DimX/2, y=-self.Properties.DimY/2, z=-self.Properties.DimZ/2 };
  100.     local Max = { x=self.Properties.DimX/2, y=self.Properties.DimY/2, z=self.Properties.DimZ/2 };
  101.     self:SetBBox( Min, Max );
  102.     
  103.  
  104.     self.Who = nil;
  105.     self.UpdateCounter = 0;
  106.     self.Entered = 0;
  107.  
  108.  
  109.     if(self.Properties.bEnabled==1)then
  110.         self:GotoState( "Inactive" );
  111.         self:GotoState( "Empty" );
  112.     else
  113.         self:GotoState( "Empty" );
  114.         self:GotoState( "Inactive" );
  115.     end
  116.  
  117.  
  118. end
  119.  
  120. function AITrigger:Event_Enter( sender )
  121.     if ((self.Entered ~= 0)) then
  122.         return
  123.     end
  124.  
  125.     if (sender.cnt and (sender.type=="Player")) then 
  126.         self.Who = sender;
  127.     elseif (sender and sender.Who) then 
  128.         self.Who = sender.Who;
  129.     end
  130.  
  131.     self:Event_Signal(self);
  132.  
  133.     self.bTriggered = 1;
  134.     self.Entered = 1;
  135.  
  136.  
  137.     BroadcastEvent( self,"Enter" );
  138.  
  139.     if (self.Properties.bTriggerOnce==1) then
  140.         self:GotoState( "Inactive" );
  141.     end
  142.  
  143.  
  144.  
  145.     if ( sender ) then
  146.         self:Log( "Player "..sender:GetName().." Enter AITrigger "..self:GetName() );
  147.     end
  148. end
  149.  
  150. function AITrigger:Event_Leave( sender )
  151.     if (self.Entered == 0) then
  152.         return
  153.     end
  154.     self.Entered = 0;
  155.     BroadcastEvent( self,"Leave" );
  156.     if (sender ~= nil) then
  157.         self:Log( "Player "..sender:GetName().." Leave AITrigger "..self:GetName() );
  158.     end
  159. end
  160.  
  161. function AITrigger:Event_Enable( sender )
  162.     self:GotoState("Empty")
  163.     if (self.Properties.aianchorAIAction~="") then
  164.         AI:RegisterWithAI(self.id, AIAnchor[self.Properties.aianchorAIAction], self.Properties.fAnchorRadius);
  165.     end
  166.  
  167.     BroadcastEvent( self,"Enable" );
  168. end
  169.  
  170. function AITrigger:Event_Signal( sender )
  171.  
  172.  
  173.  
  174.     if ((self.Properties.Signal.bSendSignal == 1) and (self.Who)) then
  175.             
  176.         if (self.Properties.Signal.bReadibility==1) then
  177.             AI:Signal(SIGNALID_READIBILITY, 1, self.Properties.Signal.signalText,self.Who.id);
  178.         else        
  179.             AI:FreeSignal(1,self.Properties.Signal.signalText,self:GetPos(),self.Properties.Signal.signalRadius,self.Who.id); 
  180.         end
  181.     end
  182.  
  183.     BroadcastEvent( self,"Signal" );
  184. end
  185.  
  186. function AITrigger:Event_Disable( sender )
  187.     self:GotoState( "Inactive" );
  188.     AI:RegisterWithAI(self.id, 0);
  189.     self.bExplicitlyDisabled = 1;
  190.     BroadcastEvent( self,"Disable" );
  191. end
  192.  
  193. function AITrigger:OnEvent( EventId, Params)
  194.     if (EventId == ScriptEvent_Activate) then
  195.         self:GotoState("Empty");
  196.     end
  197. end
  198.  
  199. function AITrigger:Log( msg )
  200.     System:LogToConsole( msg )
  201. end
  202.  
  203.  
  204. -------------------------------------------------------------------------------
  205. -- Inactive State -------------------------------------------------------------
  206. -------------------------------------------------------------------------------
  207. AITrigger.Inactive =
  208. {
  209.     OnBeginState = function( self )
  210.  
  211.         if (self.bExplicitlyDisabled==0) then 
  212.  
  213.             if (self.Properties.bTriggerOnce == 0) then 
  214.                 if (self.Properties.aianchorAIAction~="") then
  215.                     AI:RegisterWithAI(self.id, AIAnchor[self.Properties.aianchorAIAction], self.Properties.fAnchorRadius);
  216.                 end
  217.             else
  218.                 if (self.Who) then
  219.                     AI:RegisterWithAI(self.id, 0);
  220.                 else
  221.                     if (self.Properties.aianchorAIAction~="") then
  222.                         AI:RegisterWithAI(self.id, AIAnchor[self.Properties.aianchorAIAction], self.Properties.fAnchorRadius);
  223.                     end    
  224.                 end
  225.             end
  226.         end
  227.         
  228.  
  229.         if (self.Properties.bStopAI == 1) then
  230.  
  231.             if (self.Who) then
  232.                 AI:Signal(0,1,"EXIT_WAIT_STATE",self.Who.id);
  233.             end
  234.     
  235.             for i,value in self.Waiting do
  236.                 if (value==player) then
  237.                     do return end
  238.                 end
  239.                 AI:Signal(0,1,"EXIT_WAIT_STATE",value.id);
  240.                 self.Waiting[i] = nil;
  241.             end
  242.             self.NumWaiting = 0;
  243.         end
  244.  
  245.     end,
  246.     OnEndState = function( self )
  247.     end,
  248.     OnEvent = AITrigger.OnEvent,
  249. }
  250. -------------------------------------------------------------------------------
  251. -- Empty State ----------------------------------------------------------------
  252. -------------------------------------------------------------------------------
  253. AITrigger.Empty =
  254. {
  255.     -------------------------------------------------------------------------------
  256.     OnBeginState = function( self )
  257.  
  258.         self.Who = nil;
  259.         self.UpdateCounter = 0;
  260.         self.Entered = 0;
  261.  
  262.  
  263.  
  264.     end,
  265.  
  266.     OnEndState = function( self )
  267.         AI:RegisterWithAI(self.id, 0);
  268.     end,
  269.  
  270.     OnTimer = function( self )
  271.         self:GotoState( "Occupied" );
  272.     end,
  273.  
  274.     -------------------------------------------------------------------------------
  275.     OnContact = function( self,player )
  276.         -- Ignore if disabled.
  277.         if (player.ai == nil) then
  278.             return
  279.         end
  280.  
  281.         if (player.cnt.health<=0) then
  282.             return
  283.         end
  284.  
  285.         self.Who = player;
  286.         self:GotoState( "Occupied" );
  287.  
  288.     end,
  289.  
  290.     -------------------------------------------------------------------------------
  291.     OnEnterArea = function( self,player,areaId )
  292.         -- if Only for AI, then check
  293.         if (player.ai == nil) then
  294.             return
  295.         end
  296.  
  297.  
  298.         if (player.cnt.health<=0) then
  299.             return
  300.         end
  301.  
  302.     
  303.         if (self.Who == nil) then
  304.             self.NumInside = self.NumInside + 1;
  305.             self.Who = player;
  306.             self:GotoState( "Occupied" );
  307.         end
  308.     end,
  309.     OnEvent = AITrigger.OnEvent,
  310.  
  311. }
  312.  
  313. -------------------------------------------------------------------------------
  314. -- Empty State ----------------------------------------------------------------
  315. -------------------------------------------------------------------------------
  316. AITrigger.Occupied =
  317. {
  318.     -------------------------------------------------------------------------------
  319.     OnBeginState = function( self )
  320.  
  321.         self:Event_Enter(self.Who);
  322.  
  323.         if (self.Properties.bStopAI == 1) then
  324.             AI:Signal(0,1,"GO_INTO_WAIT_STATE",self.Who.id);
  325.         end
  326.  
  327.  
  328.         
  329.     end,
  330.  
  331.     -------------------------------------------------------------------------------
  332.     OnContact = function( self,player )
  333.         -- Ignore if disabled.
  334.  
  335.         -- if Only for AI, then check
  336.         if (player.ai == nil) then
  337.             return
  338.         end
  339.  
  340.  
  341.         if (self.Properties.bAILadder == 1) then 
  342.             player.cnt:SetGravity(0.0);
  343.         end
  344.  
  345.  
  346.         --add a very small delay(so is immediate)
  347.         if(self.Properties.ExitDelay==0) then
  348.             self.Properties.ExitDelay=0.01;
  349.         end
  350.  
  351.         --self:SetTimer(self.Properties.ExitDelay*1000);
  352.  
  353.  
  354.         if ( (player~=self.Who) and (self.Properties.bStopAI == 1) ) then 
  355.             
  356.             if (self.NumWaiting>0) then
  357.                 -- look for this guy
  358.                 for i,value in self.Waiting do
  359.                     if (value==player) then
  360.                         do return end
  361.                     end
  362.                 end
  363.                 self.NumWaiting = self.NumWaiting + 1;
  364.                 self.Waiting[self.NumWaiting] = player;
  365.             else
  366.                 self.NumWaiting = 1;
  367.                 self.Waiting[1] = player;
  368.             end
  369.  
  370.             AI:Signal(0,1,"GO_INTO_WAIT_STATE",player.id);
  371.         end
  372.     end,
  373.  
  374.     -------------------------------------------------------------------------------
  375.     OnTimer = function( self )
  376.         --self:Log("Sending on leave");
  377.         --self:Event_Leave( self,self.Who );
  378.         --self:GotoState("Empty");
  379.     end,
  380.     
  381.     OnEnterArea = function( self,player,areaId )
  382.     
  383.     -- if Only for AI, then check
  384.         if (player.ai == nil) then
  385.             return
  386.         end
  387.  
  388.         if (player.cnt.health<=0) then
  389.             return
  390.         end
  391.  
  392.         
  393.         self.NumInside = self.NumInside + 1;
  394.     end,
  395.  
  396.     -------------------------------------------------------------------------------
  397.     OnLeaveArea = function( self,player,areaId )
  398.     
  399.     
  400.     -- if Only for AI, then check
  401.         if (player.ai == nil) then
  402.             return
  403.         end
  404.  
  405.         if (player.cnt.health<=0) then
  406.             return
  407.         end
  408.  
  409.         
  410.         self.NumInside = self.NumInside - 1;
  411.         if (self.NumInside <= 0) then
  412.             --self:Log("Sending on leave");
  413.             self:Event_Leave( self,self.Who );
  414.             self:GotoState("Empty");
  415.         end
  416.     end,
  417.  
  418.     OnEvent = AITrigger.OnEvent,
  419.  
  420. }